<?php
namespace zhtfrontend\controllers;

use common\components\helpers\Util;
use common\models\datas\Type;
use yii\db\Query;
use zhtfrontend\components\bases\BaseController;
use zhtfrontend\components\helpers\Funs;
use zhtfrontend\components\services\goods\SetDistributionAction;
use zhtfrontend\components\services\goods\ShelveAction;
use zhtfrontend\components\services\goods\UpdateAction;
use zhtfrontend\models\datas\Arrangement;
use zhtfrontend\models\datas\ArtRelation;
use zhtfrontend\models\datas\ArtTeaching;
use zhtfrontend\models\datas\Classroom;
use zhtfrontend\models\datas\Course;
use zhtfrontend\models\datas\GoodsClassTime;
use zhtfrontend\models\datas\GoodsGroup;
use yii\web\BadRequestHttpException;
use yii\helpers\Json;
use yii\web\Response;
use zhtfrontend\models\datas\Goods;
use common\models\datas\Cate;
use yii\data\Pagination;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\GoodsAttr;
use yii\widgets\ActiveForm;
use common\models\datas\CateAttr;
use common\components\helpers\Tree;
use common\models\datas\TypeBrand;
use common\models\datas\Brand;
use yii\helpers\ArrayHelper;
use zhtfrontend\models\datas\Mechanic;
use yii\web\NotFoundHttpException;
use zhtfrontend\models\datas\Teacher;
use zhtfrontend\models\datas\Tutor;

class GoodsskillController extends BaseController{

    public function actions(){
        return [
            'shelve'=>[
                'class'=>ShelveAction::className(),
            ],
            'set'=>[
                'class'=>SetDistributionAction::className(),
            ],
            'update'=>[
                'class'=>UpdateAction::className(),
            ],
        ];
    }

    public function actionIndex()
    {
           $tree=$this->getCategory(1);
        $search_data=\Yii::$app->request->get();
            $tree = str_replace('&nbsp;',' ' ,$tree);
            $pageSize = !empty($param['pageSize']) ? $param['pageSize'] : 12;
            $goods=Goods::find()->where(['type_id'=>1,'is_delete'=>0,'is_group'=>1,'teaching_methods'=>1]);
            if (!empty(\Yii::$app->request->get('goods_name'))) {
                $goods->andWhere(['like', 'name', \Yii::$app->request->get('goods_name')]);
            }
            if (!empty($search_data['cate_name']) ){
                $cateids[]=$search_data['cate_name'];
                $cate=Cate::findOne($search_data['cate_name']);
                $cates = [];
                if($cate->parent_id == 0){
                    $cates = Cate::find()->select('id')->where(['parent_id'=>$search_data['cate_name']])->column();
                }
                $cateids = array_merge($cates,$cateids);
                $cate = Cate::find()->select('id')->Where(['in', 'id', $cateids])->column();
                $goods->andWhere(['in', 'cat_id', $cate]);
            }
            if (!empty($search_data['mechanic_name'])) {
                $mechanic=Mechanic::find()->select('id')->where(['like','name',$search_data['mechanic_name']])->column();
                $goods->andWhere(['in', 'mechanic_id', $mechanic]);
            }

            $pages = new Pagination(['totalCount' => $goods->count(), 'pageSize' => $pageSize,'params'=>array_merge($_GET,$search_data)]);
            isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
            $model = $goods->offset($pages->offset)->limit($pages->limit)->all();
            $array = Fun::asArray($model, ['cate_name','mechanic_name','class_time','operate']);
            foreach ($array as $key=>$val){
                $group = GoodsGroup::find()->where(['goods_id'=>$val['id']])->one();
                $array[$key]['group'] = null;
                if($group != null){
                    $array[$key]['group'] = ArtTeaching::findOne($group->group_id);
                }
            }
        return $this->render('index',['tree'=>$tree,'data'=>$array,'pages'=>$pages]);
    }
    public function getCategory($type_id){
        $site_id=Fun::getSiteId();
        $cate_attr= CateAttr::find()->select(['cat_id'])->where(['or',['mobile_is_show'=>1],['pc_is_show'=>1]])->andWhere(['site_id'=>$site_id])->orderBy('pc_sort asc')->column();
        $category=Cate::find()->indexBy('id')->where(['in','id',$cate_attr])->andWhere(['type_id'=>$type_id,'is_delete'=>0])->asArray()->all();
        //$category = Cate::find()->indexBy('id')->where(['is_delete'=>0,'type_id'=>$type_id])->asArray()->all();
        $tree = (new Tree($category))->get_tree_select();
        return  $tree;
    }

    public function actionCreate(){
        $type_id=5;//5是商品中的组合商品的类型
        $model=new Goods();
        $model->scenario = 'update';
        $model->type_id = 5;
        $attribute=Funs::getAttribute($type_id);
        $site_id=Fun::getSiteId();
        //获取全部的分类
        $cate_attr=CateAttr::find()->select(['cat_id'])->where(['or',['mobile_is_show'=>1],['pc_is_show'=>1]])->andWhere(['site_id'=>$site_id])->orderBy('pc_sort asc')->column();
        $category=Cate::find()->indexBy('id')->where(['in','id',$cate_attr])->andWhere(['is_delete'=>0])->asArray()->all();
        $cat_tree=(new Tree($category))->get_tree_select();
        //获取全部的品牌
        $brand_id=TypeBrand::find()->select(['brand_id'])->where(['type_id'=>$type_id])->column();
        $brand=Brand::find()->where(['in','id',$brand_id])->all();
        $brand_tree = ArrayHelper::map($brand, 'id', 'name');
        //获取全部的机构
        $mids = Fun::getMechanicIds();
        $mechanicModel=Mechanic::find()->where(['site_id'=>Fun::getSiteId(),'is_delete'=>0]);
        if($mids){
            $mechanicModel->andWhere(['id'=>$mids]);
        }
        $mechanic = $mechanicModel->indexBy('id')->all();
        $mechanic_tree=(new Tree($mechanic))->get_tree_select();

        //$mechanic=Mechanic::find()->where(['site_id'=>$site_id])->asArray()->all();
       // $mechanic_tree = ArrayHelper::map($mechanic, 'id', 'name');
        //获取选择选择是否上架的选项
        $shelve_array=['1'=>'上架','2'=>'暂不上架'];
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post())&& $model->validate()) {
            $post=\Yii::$app->request->post();
            $model->sn = 'product'.time();//商品编号
            $model->site_id = Fun::getSiteId();
            $model->goods_type='group';
            $model->admin_id=\Yii::$app->user->id;
            $model->type_id=5;
            $model->picture = '';
            if($model->is_shelve==1){
                $model->up_at=time();
            }
            if(isset($post['Goods']['imageList']) && !empty($post['Goods']['imageList'])){
                $imagelist = $post['Goods']['imageList'];
                $imagelist_arr = [];
                if(!empty($imagelist)){
                    $imagelist_arr = explode('#|#',$imagelist);
                }
                if(!empty($post['Goods']['defaulturl'])&&in_array($post['Goods']['defaulturl'],$imagelist_arr)){
                    $model->picture = $post['Goods']['defaulturl'];
                }else{
                    if(count($imagelist_arr)>0){
                        $model->picture = $imagelist_arr[0];//上传的列表第一个作为展示的默认图
                    }
                }
            }
            if ($model->save(false)) {
                if(!empty($post['Goods']['groupIds'])) {
                    foreach ($post['data'] as $key=>$val){
                        $data_arr =  explode(",",$val);
                        $goodsgroup = new GoodsGroup();
                        $goodsgroup->group_id = $key;
                        $goodsgroup->goods_id = $model->id;
                        $goodsgroup->discount_type = $data_arr[0];
                        $goodsgroup->price_reduce = $data_arr[1];
                        $goodsgroup->cost_reduce = $data_arr[2];
                        $goodsgroup->market_price_reduce = $data_arr[3];
                        $goodsgroup->save(false);
                    }
                }
                if(!empty($post['attr_value'])){
                    $attribute = $post['attribute'];
                    foreach($post['attr_value'] as $k=>$v){
                        if(!empty($v)){//非空的扩展属性值进行保存
                            $attributes = new GoodsAttr(['goods_id'=>$model->id,'attribute_id'=>$k,'name'=>$attribute[$k],'value'=>trim($v),'obj_type'=>'attribute']);
                            $attributes->save(false);
                        }
                    }
                }
                return $this->redirectMessage('添加成功', Fun::url(['goodsgroup/index','type_id' => 5]));
            }
        }
        return $this->render('create',['model'=>$model,'cat_tree'=>$cat_tree,'brand_tree'=>$brand_tree,'mechanic_tree'=>$mechanic_tree,'shelve_array'=>$shelve_array,'attribute'=>$attribute]);
    }
    public function actionUpdate(){
        $type_id=1;
        $id=\Yii::$app->request->get('id');
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '请求错误'));
        }
        $model =Goods::findOne(\Yii::$app->request->get('id'));
        $model->scenario = 'update';
        if ($model == null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        //获取选择的商品的相关的信息
        $group_ids=GoodsGroup::find()->select('group_id')->where(['goods_id'=>$id])->column();

        $site_id=Fun::getSiteId();
        $model->groupIds = implode(',',$group_ids);

        //获取全部的分类
        $cate_attr=CateAttr::find()->select(['cat_id'])->where(['or',['mobile_is_show'=>1],['pc_is_show'=>1]])->andWhere(['site_id'=>$site_id])->orderBy('pc_sort asc')->column();
        $category=Cate::find()->indexBy('id')->where(['in','id',$cate_attr])->andWhere(['is_delete'=>0])->asArray()->all();
        $cat_tree=(new Tree($category))->get_tree_select();
        //获取全部的品牌
        $brand_id=TypeBrand::find()->select(['brand_id'])->where(['type_id'=>$type_id])->column();
        $brand=Brand::find()->where(['in','id',$brand_id])->all();
        $brand_tree = ArrayHelper::map($brand, 'id', 'name');
        //获取全部的机构

        $mids = Fun::getMechanicIds();
        $mechanicModel=Mechanic::find()->where(['site_id'=>Fun::getSiteId(),'is_delete'=>0]);
        if($mids){
            $mechanicModel->andWhere(['id'=>$mids]);
        }
        $mechanic = $mechanicModel->indexBy('id')->all();
        $mechanic_tree=(new Tree($mechanic))->get_tree_select();
       // $mechanic=Mechanic::find()->where(['site_id'=>$site_id])->asArray()->all();
       // $mechanic_tree = ArrayHelper::map($mechanic, 'id', 'name');
        //获取选择选择是否上架的选项
        $shelve_array=['0'=>'下架','1'=>'上架','2'=>'暂不上架'];
        $data=[];
        $group_goods=Goods::find()->where(['id'=>$group_ids])->all();
        if($group_goods) {
            foreach ($group_goods as $key => $value) {
                $data[] = $value->toArray([], ['cate_name', 'mechanic_name', 'brand_name', 'discount_type', 'price_reduce', 'cost_reduce', 'market_price_reduce']);
            }
        }
//        $default_json = json_encode($data, JSON_UNESCAPED_UNICODE);
        //保存产品的扩展属性
        $attribute=Funs::getAttribute($type_id,$id);
        $postdata=\Yii::$app->request->post();
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $post = \Yii::$app->request->post();
            if($model->is_shelve==1){
                $model->up_at=time();
            }elseif($model->is_shelve==0){
                $model->down_at=time();
            }else{
                $model->down_at=0;
            }
            if(isset($post['Goods']['imageList']) && !empty($post['Goods']['imageList'])){
                $imagelist = $post['Goods']['imageList'];
                $imagelist_arr = [];
                if(!empty($imagelist)){
                    $imagelist_arr = explode('#|#',$imagelist);
                }
                if(!empty($post['Goods']['defaulturl'])&&in_array($post['Goods']['defaulturl'],$imagelist_arr)){
                    $model->picture = $post['Goods']['defaulturl'];
                }else{
                    if(count($imagelist_arr)>0){
                        $model->picture = $imagelist_arr[0];//上传的列表第一个作为展示的默认图
                    }
                }
            }
            if ($model->save(false)) {
                if(!empty($post['data']) && !empty($post['Goods']['groupIds'])){
                    foreach($post['data'] as $key=>$val){
                        $data_arr = explode(",",$val);
                        $group = GoodsGroup::findOne(['goods_id'=>$model->id,'group_id'=>$key]);
                        if($group == null){
                            $goodsgroup = new GoodsGroup();
                            $goodsgroup->group_id = $key;
                            $goodsgroup->goods_id = $model->id;
                            $goodsgroup->discount_type = $data_arr[0];
                            $goodsgroup->price_reduce = $data_arr[1];
                            $goodsgroup->cost_reduce = $data_arr[2];
                            $goodsgroup->market_price_reduce = $data_arr[3];
                            $goodsgroup->save(false);
                        }else{
                            $group->discount_type = $data_arr[0];
                            $group->price_reduce = $data_arr[1];
                            $group->cost_reduce = $data_arr[2];
                            $group->market_price_reduce = $data_arr[3];
                            $group->save(false);
                        }
                    }
                    $post_group = explode(",", $post['Goods']['groupIds']);
                    foreach($group_ids as $gid){
                        if(!in_array($gid,$post_group)){
                            $group = GoodsGroup::findOne(['goods_id'=>$model->id,'group_id'=>$gid]);
                            $group->delete();
                        }
                    }
                }

                if(!empty($postdata['attr_value'])){
                    foreach($postdata['attr_value'] as $k=>$v){
                        /*
                         * 遍历获取填写的扩展属性，到库表中查该model该属性id是否已经存在了值了，如果存在了，则更新其value
                         * 如果不存在，则执行插入操作
                         */
                        $goodattr = GoodsAttr::findOne(['goods_id'=>$model->id,'attribute_id'=>$k]);
                        if($goodattr == null){//执行插入操作
                            $goodattrnew = new GoodsAttr(['goods_id'=>$model->id,'attribute_id'=>$k,'value'=>$v,'obj_type'=>'attribute']);
                            $goodattrnew->save(false);
                        }else{//执行更新操作
                            $goodattr->value = $v;
                            $goodattr->save(false);
                        }
                    }
                }//保存完成商品的扩展属性
                return $this->redirectMessage('修改成功', Fun::url(['goodsgroup/index','type_id' => 5]));
            }
        }
        return $this->render('create',['model'=>$model,'cat_tree'=>$cat_tree,'brand_tree'=>$brand_tree,'mechanic_tree'=>$mechanic_tree,'goodsgroup'=>$data,'shelve_array'=>$shelve_array,'attribute'=>$attribute]);
    }

    public function actionAdd(){
        $id=\Yii::$app->request->get('id');
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '请求错误'));
        }
        $site_id = Fun::getSiteId();
        $model =Goods::findOne($id);
        $model->scenario = 'addskill';
        if ($model == null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        //获取全部的分类
        $cate_attr=CateAttr::find()->select(['cat_id'])->where(['or',['mobile_is_show'=>1],['pc_is_show'=>1]])->andWhere(['site_id'=>$site_id])->orderBy('pc_sort asc')->column();
        $category=Cate::find()->indexBy('id')->where(['in','id',$cate_attr])->andWhere(['is_delete'=>0])->asArray()->all();
        $cat_tree=(new Tree($category))->get_tree_select();

        $attribute=Funs::getAttribute($model->type_id,$id);
        $artModel = ArtTeaching::find()->where(['type'=>$model->category_id,'status'=>1])->all();
        $artList = ArrayHelper::map($artModel,'id','title');

        $data[] = $model->toArray([], ['cate_name', 'mechanic_name', 'brand_name', 'class_time','class_count']);

        $artModel = [];
        $goodsGroup = GoodsGroup::find()->where(['goods_id'=>$id])->one();
        if($goodsGroup != null){
            $artModel = ArtTeaching::find()->where(['id'=>$goodsGroup->group_id])->asArray()->one();
            $artModel['discount_type'] = $goodsGroup->discount_type;
            $artModel['price_reduce'] = $goodsGroup->price_reduce;
            $model->groupIds = $artModel['id'];
            $model->group_price = $goodsGroup->price;
        }

        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $post = \Yii::$app->request->post();
            $discount_type = $post['discount_type'];
            $price_reduce = $post['price_reduce'];
            $model->discount_type = $discount_type[$id];
            $model->reduce = $price_reduce[$id];
            if($model->save(false)){
                $groupModel = GoodsGroup::find()->where(['goods_id'=>$id])->one();
                if($groupModel == null) {
                    $groupModel = new GoodsGroup();
                }
                $groupModel->goods_id = $id;
                $groupModel->group_id = (int)$model->groupIds;
                $groupModel->discount_type = $discount_type[$model->groupIds];
                $groupModel->price_reduce = $price_reduce[$model->groupIds];
                $groupModel->cost_reduce = $price_reduce[$model->groupIds];
                $groupModel->market_price_reduce = $price_reduce[$model->groupIds];
                $groupModel->price = $model->group_price;
                $groupModel->save(false);
                return $this->redirectMessage('操作成功', Fun::url(['goodsskill/index']));
            }
        }
        $tutor = Tutor::find()->where(['status'=>1])->all();
        $tutor = ArrayHelper::map($tutor,'id','name');

        return $this->render('add',['data'=>$data,'tutor'=>$tutor, 'model'=>$model,'cat_tree'=>$cat_tree,'attribute'=>$attribute,'artList'=>$artList,'artModel'=>$artModel]);
    }

    public function actionArt(){
        \Yii::$app->response->format = Response::FORMAT_JSON;
        $art_id = \Yii::$app->request->get('id');
        $row = ArtTeaching::find()->where(['id'=>$art_id])->asArray()->one();
        $row['class_time'] = Util::getWeek($row['class_day']).' '.$row['star_time'].'-'.$row['end_time'];
        if($row['type'] == 1){
            $row['cat_name'] = '益智';
        }elseif ($row['type'] == 2){
            $row['cat_name'] = '音乐';
        }else{
            $row['cat_name'] = '形体';
        }

        return $row;
    }
    public function actionViewgroup(){
        $this->layout='@app/views/layouts/iframe.php';
        $art_id = \Yii::$app->request->get('id');
        $model = ArtTeaching::findOne($art_id);
        $arrangement = (new \yii\db\Query())->select(['ar.*','a.*'])
                      ->from('{{%art_relation}} ar')
                      ->leftJoin('{{%arrangement}} a','a.id = ar.arrangement_id')
                      ->where(['ar.art_teaching_id'=>$art_id])
                      ->orderBy('ar.lesson ASC')
                      ->all(Fun::getSiteDb());
        $data = [];
        foreach ($arrangement as $item){
                $teacher = Teacher::findOne($item['teacher_id']);
                $coures = Course::findOne($item['course_id']);
                $classroom = Classroom::findOne($item['classroom_id']);
                $row['coures_name'] =isset($coures)?$coures->name:'';
                $row['teacher_name'] =isset($teacher)?$teacher->name:'';
                $row['classroom_name'] =isset($classroom)?$classroom->name:'';
                $row['lesson'] = $item['lesson'];
                $row['class_time'] = Util::getWeek($model->class_day).' '.$model->star_time.'-'.$model->end_time;
                $data[] = $row;
        }
        return $this->render('viewgroup',['data'=>$data]);
    }


    public function actionAddgroup(){
        $this->layout='@app/views/layouts/iframe.php';
        $goods_id = \Yii::$app->request->get('id');
        if (\Yii::$app->request->isAjax) {
            if (\Yii::$app->request->post('param') != null) $param = Json::decode(\Yii::$app->request->post('param'));
            \Yii::$app->response->format = Response::FORMAT_JSON;
            $pageSize = !empty($param['pageSize']) ? $param['pageSize'] : 12;
            $goodsRow = Goods::findOne($goods_id);
            $goods=Goods::find()->where(['is_delete'=>0,'is_show'=>1,'teaching_methods'=>2,'category_id'=>$goodsRow->category_id]);
            $array = [];
            $pages = null;
            if (!empty($param['searchData'])) {
                $search_data = array_filter($param['searchData']);
                if (!empty($search_data['goods_name'])) {
                    $goods->andWhere(['like', 'name', $search_data['goods_name']]);
                }
                $pages = new Pagination(['totalCount' => $goods->count(), 'pageSize' => $pageSize]);

            }
            isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
            $model = $goods->offset($pages->offset)->limit($pages->limit)->all();
            $array = Fun::asArray($model, ['checkbox','cate_name','mechanic_name','class_time','class_count']);
            $ajax_object = new AjaxObject();
            $ajax_object->data =$array;
            $ajax_object->pages = $pages;
            return $ajax_object;
        }
        return $this->render('addgroup');
    }

    public function actionDelete(){
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        $id = \Yii::$app->request->get('id');
        $model = Goods::findOne(\Yii::$app->request->get('id'));
        if ($model == null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        if ($model->delete()) {
            return $this->redirectMessage(\Yii::t('zhtfrontend', '信息删除成功'), Fun::url(['goodsskill/index']));
        }
    }
    public function actionDetails(){
        //$type_id=5;
        $site_id = Fun::getSiteId();
        $id = \Yii::$app->request->get('id');
        if ($id == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '请求错误'));
        }
        $goods =Goods::findOne($id);
        if ($goods == null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        $category=null;$brand=null;$mechanic=null;

        if($goods->cat_id != null){
            $category = Cate::findOne($goods->cat_id);
        }
        if($goods->brand_id != null){
            $brand = Brand::findOne($goods->brand_id);
        }
        if($goods->mechanic_id != null){
            $mechanic = Mechanic::findOne(['id'=>$goods->mechanic_id,'site_id'=>$site_id]);
        }
        $AttributeDetails = Funs::getExtendattributeDetails(1,$id);
        $specialdatas= $this->getSpecialModuleDatas(1, $id);
        $goodsGroup = GoodsGroup::find()->where(['goods_id'=>$id])->one();
        $data = [];
        $modelArtTeaching = null;
        if($goodsGroup != null){
            $modelArtTeaching = ArtTeaching::findOne($goodsGroup->group_id);
        }
        return $this->render('details',['model'=>$goods,'specialdatas'=>$specialdatas,'category'=>$category,'brand'=>$brand,'mechanic'=>$mechanic,'AttributeDetails'=>$AttributeDetails,'modelart'=>$modelArtTeaching,'items'=>$data]);
    }

    //获取某种类型商品特有的扩展数据
    public function getSpecialModuleDatas($typeid, $goodid){
        if($goodid == null || $typeid == null){
            return null;
        }
        $good = Goods::findOne($goodid);
        if(empty($good)){
            return null;
        }
        if($typeid == 1){//课程产品
            //$arrange = Arrangement::findOne($good->arrangement_id)->toArray([],['course_name','teacher_name','room_number','class_day','start_time','end_time']);//根据排课id获取排课记录
            $goodsarr_query = (new Query())->select(["goods_id","max(IF(name='课程',value,'')) as course_name","max(IF(name='老师',value,'')) as teacher_name","max(IF(name='教室编号',value,'')) as room_number","max(IF(name='教室名称',value,'')) as room_name","max(IF(name='开课人数',value,'')) as opening_number","max(IF(name='最满人数',value,'')) as max_number",
                "max(IF(name='是否可预约',value,'')) as is_booking","max(IF(name='上课次数',value,'')) as class_count"])->from('{{%goods_attr}}')->groupBy('goods_id')->having(['goods_id'=>$good->id])->one(Fun::getSiteDb());
            //$array = Fun::asArray($arrange,['course_name','teacher_name','room_number','class_day','start_time','end_time']);
            $str = '';

            $times = GoodsClassTime::find()->where(['goods_id'=>$goodid])->asArray()->all();
            if($times){
                foreach($times as $t){
                    $str .= Util::getWeek($t['class_day']).$t['start_time'].'-'.$t['end_time'];
                }
            }
            $goodsarr_query['class_day'] = $str;
            return $goodsarr_query;
        }else{
            //实现其他类型商品的特殊数据
            return null;
        }
    }

}